<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Cache,Event,Auth,Excel,Image;
class KaoheController extends Controller {

    public function company()
    {
        $res = DB::table('baoan_company_kh')->orderby('paixu','asc')->paginate(10);
        $count = DB::table('baoan_company_kh')->count();

        return view('admin.kaohe.company', ['res' => $res,'count'=>$count]);
    }

    public function add_company(Request $request)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token','daan','fenshu');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            $kaohe_id = DB::table('baoan_company_kh')->insertGetId($data);
            //处理答案
            $daan = Input::get('daan');
            $fenshu = Input::get('fenshu');
            $res = array_combine($daan,$fenshu);
            foreach ($res as $k=>$v){
                if($v){
                    $arr['pid'] = $kaohe_id;
                    $arr['val'] = $k;
                    $arr['mark'] = $v;
                    DB::table('baoan_company_kh_daan')->insert($arr);
                }
            }
            return redirect('admin/kaohe/company');
        }
        return view('admin.kaohe.add_company');
    }

    public function edit_company($id,Request $request)
    {
        $re = DB::table('baoan_company_kh')->where('id',$id)->first();
        if($request->isMethod('post')){
            $data = Input::except('_token','daan','fenshu');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            //处理答案
            $daan = Input::get('daan');
            $fenshu = Input::get('fenshu');
            $res = array_combine($daan,$fenshu);

            //先删除原有答案
            DB::table('baoan_company_kh_daan')->where('pid',$id)->delete();
            //再添加新的答案
            foreach ($res as $k=>$v){
                if ($v) {
                    $arr['pid'] = $id;
                    $arr['val'] = $k;
                    $arr['mark'] = $v;
                    DB::table('baoan_company_kh_daan')->insert($arr);
                }
            }
            DB::table('baoan_company_kh')->where('id',$id)->update($data);
            return redirect('admin/kaohe/company');
        }
        //答案
        $res = DB::table('baoan_company_kh_daan')->where('pid',$id)->get();
        if(count($res) != 0){
            foreach ($res as $k=>$v){
                $list[$k+1] = $v->val;
                $mark[$k+1] = $v->mark;
            }
        }else{
            $list = array();
            $mark = array();
        }

        return view('admin.kaohe.edit_company', ['list'=>$list,'mark'=>$mark,'re' => $re]);
    }

    public function del_company($id)
    {
        db::table('baoan_company_kh')->where('id',$id)->delete();
        db::table('baoan_company_kh_daan')->where('pid',$id)->delete();
        return redirect()->back();
    }

    public function baoan()
    {
        $res = DB::table('baoan_kh')->orderby('paixu','asc')->paginate(10);
        $count = DB::table('baoan_kh')->count();

        return view('admin.kaohe.baoan', ['res' => $res,'count'=>$count]);
    }

    public function add_baoan(Request $request)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token','daan','fenshu');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            $kaohe_id = DB::table('baoan_kh')->insertGetId($data);
            //处理答案
            $daan = Input::get('daan');
            $fenshu = Input::get('fenshu');
            $res = array_combine($daan,$fenshu);
            foreach ($res as $k=>$v){
                if($v){
                    $arr['pid'] = $kaohe_id;
                    $arr['val'] = $k;
                    $arr['mark'] = $v;
                    DB::table('baoan_kh_daan')->insert($arr);
                }
            }
            return redirect('admin/kaohe/baoan');
        }
        return view('admin.kaohe.add_baoan');
    }

    public function edit_baoan($id,Request $request)
    {
        $re = DB::table('baoan_kh')->where('id',$id)->first();
        if($request->isMethod('post')){
            $data = Input::except('_token','daan','fenshu');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            //处理答案
            $daan = Input::get('daan');
            $fenshu = Input::get('fenshu');
            $res = array_combine($daan,$fenshu);

            //先删除原有答案
            DB::table('baoan_kh_daan')->where('pid',$id)->delete();
            //再添加新的答案
            foreach ($res as $k=>$v){
                if ($v) {
                    $arr['pid'] = $id;
                    $arr['val'] = $k;
                    $arr['mark'] = $v;
                    DB::table('baoan_kh_daan')->insert($arr);
                }
            }
            DB::table('baoan_kh')->where('id',$id)->update($data);
            return redirect('admin/kaohe/baoan');
        }
        //答案
        $res = DB::table('baoan_kh_daan')->where('pid',$id)->orderby('id','asc')->get();
        if(count($res) != 0){
            foreach ($res as $k=>$v){
                $list[$k+1] = $v->val;
                $mark[$k+1] = $v->mark;
            }
        }else{
            $list = array();
            $mark = array();
        }

        return view('admin.kaohe.edit_baoan', ['list'=>$list,'mark'=>$mark,'re' => $re]);
    }

    public function del_baoan($id)
    {
        db::table('baoan_kh')->where('id',$id)->delete();
        db::table('baoan_kh_daan')->where('pid',$id)->delete();
        return redirect()->back();
    }

    public function dengji(){
        $res = DB::table('baoan_dengji')->paginate(10);
        $count = DB::table('baoan_dengji')->count();

        return view('admin.kaohe.dengji', ['res' => $res,'count'=>$count]);
    }

    public function add_dengji(request $request){
        if($request->isMethod('post')){
            $data = Input::except('_token');
            DB::table('baoan_dengji')->insert($data);
            return redirect('admin/kaohe/dengji');
        }

        return view('admin.kaohe.add_dengji');
    }

    public function edit_dengji($id,request $request){
        $res = db::table('baoan_dengji')->where('id',$id)->first();
        if($request->isMethod('post')){
            $data = Input::except('_token');
            DB::table('baoan_dengji')->where('id',$id)->update($data);
            return redirect('admin/kaohe/dengji');
        }

        return view('admin.kaohe.edit_dengji',['res'=>$res]);
    }

    public function del_dengji($id)
    {
        db::table('baoan_dengji')->where('id',$id)->delete();
        return redirect()->back();
    }


}


